package com.ruowei.modules.wxpay.web.rest;

import com.ruowei.modules.wxpay.service.WeChatPayService;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/wxpay")
@Transactional
public class WeChatPayResource {
    private final WeChatPayService weChatPayService;

    public WeChatPayResource(WeChatPayService weChatPayService) {
        this.weChatPayService = weChatPayService;
    }

    @PostMapping("/notifyUrl")
    @ApiOperation(value = "统一下单回调url", notes = "必须是http post方式接收请求，否则接收不到；返回参数如果没有加密方式，使用256加密进行签名验证，不要使用MD5")
    @Transactional
    public void notifyUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            weChatPayService.orderNotify(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/refundNotify")
    @ApiOperation(value = "申请退款回调url", notes = "必须是http post方式接收请求，否则接收不到；返回参数如果没有加密方式，使用256加密进行签名验证，不要使用MD5")
    @Transactional
    public void refundNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            weChatPayService.refundNotify(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/refund")
    @ApiOperation(value = "退款测试", notes = "必须是http post方式接收请求，否则接收不到；返回参数如果没有加密方式，使用256加密进行签名验证，不要使用MD5")
    @Transactional
    public ResponseEntity<Map<String, String>> refund(BigDecimal amount, String orderCode) throws Exception {
        Map<String, String> map = new HashMap<>();
        try {
            map = weChatPayService.refund(amount, orderCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(map);
    }

    @PostMapping("/queryOrder")
    @ApiOperation(value = "查询订单状态测试", notes = "必须是http post方式接收请求，否则接收不到；返回参数如果没有加密方式，使用256加密进行签名验证，不要使用MD5")
    @Transactional
    public ResponseEntity<Map<String, String>> queryOrder(String orderCode) throws Exception {
        Map<String, String> map = new HashMap<>();
        try {
            map = weChatPayService.queryOrder(orderCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(map);
    }

    @PostMapping("/refundQuery")
    @ApiOperation(value = "查询退款状态测试", notes = "必须是http post方式接收请求，否则接收不到；返回参数如果没有加密方式，使用256加密进行签名验证，不要使用MD5")
    @Transactional
    public ResponseEntity<Map<String, String>> refundQuery(String orderCode) throws Exception {
        Map<String, String> map = new HashMap<>();
        try {
            map = weChatPayService.refundQuery(orderCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(map);
    }
}
